import 'dart:ffi';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:open_file/open_file.dart';
import '../../helper/upload_helper.dart';
import '../../helper/Helper.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/downloadCBH.dart';
import '../../helper/downloadHelper.dart';
import '../../helper/global_config.dart';
import '../../helper/iconHelper.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/ftpfile.dart';
import '../../model/space_item.dart';
import 'file_view.dart';
import '../../widgets/dialogWidget.dart';
import '../../widgets/inputWidget.dart';

import 'fileSpace.dart';

class FileSpaceHelper {
  static bool IfView(String n1) {
    if (n1.contains(".doc") ||
        n1.contains(".ppt") ||
        n1.contains(".txt") ||
        n1.contains(".PPT") ||
        n1.contains(".xls") ||
        n1.contains(".XLS") ||
        n1.contains(".DOC") ||
        n1.contains(".pdf") ||
        n1.contains(".PDF")) {
      return true;
    }
    return false;
  }
  /*
  static Widget FileCard2(
      BuildContext context, FtpFile s1, FileDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(s1.UserId);
    String _remark = Helper.DateTimeFormat2(s1.CreateTime);
    if (u1 != null) _remark = _remark + "    " + u1.Name;
    return
      Container(
     key: Key(s1.Guid),
      color: Colors.white,
        child:Column(
 
      children:[Container(

        margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
        child: ListTile(
        title: Text(s1.FileName),
        subtitle:Padding(child: Text(
          Helper.GetFileSize(s1.Size) + "    " + _remark,
          style: TextStyle(color: Colors.black38, fontSize: 14.0),
        ),
        padding: EdgeInsets.only(top:4.0),
        ),
        leading: IconHelper.getFileIcon(s1.FileName, 36.0),
        onTap: () async {
          var fPath = await DownloadHelper.GetLocalFile(s1.Guid);
          if (fPath != "") {
           if(IfView(s1.FileName)){
               RouteHelper.ShowPage(context, FileReaderPage(filePath: fPath,));
           }else{
             OpenFile.open(fPath);
           }
           
          } else {
            showDialog(
              context: context,
              builder: (BuildContext context) {
                return DialogWidget.getDialog(
                    context, "下载文件", "点击确定将该文件添加到下载目录！", () {
                  DownloadHelper.DownLoad(context, s1);
                });
              },
            );
          }
        },

        onLongPress: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              if (u1.Guid != UIData.CurrentUser!.Guid) {
                return DialogWidget.getAlert(context, "提示", "只能删除自己上传的文件");
              } else {
                return DialogWidget.getDialog(
                    context, "删除文件", "点击确定将从服务器中删除该文件！", () {
                  FtpFile.Delete(s1);
                  deleted(s1);
                });
              }
            },
          );
        },
      ),
    ),
    InputWidget.getDivider()
    ]));
  }
*/

  static Widget FileCard2(
      BuildContext context, SpaceItem s1, FileDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(s1.UserId);
    String _remark = Helper.DateTimeFormat2(s1.CreateTime);
    if (u1 != null) _remark = _remark + "    " + u1.Name;
    return Container(
        key: Key(s1.Guid),
        color: Colors.white,
        child: Column(children: [
          Container(
            margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
            child: ListTile(
              title: Text(s1.FileName),
              subtitle: Padding(
                child: Text(
                  Helper.GetFileSize(s1.Size) + "    " + _remark,
                  style: TextStyle(color: Colors.black38, fontSize: 14.0),
                ),
                padding: EdgeInsets.only(top: 4.0),
              ),
              leading: IconHelper.getFileIcon(s1.FileName, 36.0),
              onTap: () async {
                var fPath = await DownloadHelper.GetLocalFile(s1.Guid);

                if (fPath != "") {
                  if (IfView(s1.FileName)) {
                    RouteHelper.ShowPage(
                        context,
                        FileReaderPage(
                          filePath: fPath,
                        ));
                  } else {
                    OpenFile.open(fPath);
                  }
                } else {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return DialogWidget.getDialog(
                          context, "下载文件", "点击确定将该文件添加到下载目录！", () {
                        DownloadHelper.DownLoad2(context, s1);
                      });
                    },
                  );
                }
              },
              onLongPress: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    if (u1.Guid != UIData.CurrentUser!.Guid) {
                      return DialogWidget.getAlert(
                          context, "提示", "只能删除自己上传的文件");
                    } else {
                      return DialogWidget.getDialog(
                          context, "删除文件", "点击确定将从服务器中删除该文件！", () {
                        SpaceItem.Delete(s1);
                        deleted(s1);
                      });
                    }
                  },
                );
              },
            ),
          ),
          InputWidget.getDivider()
        ]));
  }

  static Widget FtpFileCard(
      BuildContext context, FtpFile s1, FtpFileDeleteCallback deleted) {
    var u1 = CacheHelper.GetUser(s1.UserId);
    String _remark = Helper.DateTimeFormat2(s1.CreateTime);
    if (u1 != null) _remark = _remark + "    " + u1.Name;
    return Container(
        key: Key(s1.Guid),
        color: Colors.white,
        child: Column(children: [
          Container(
            margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
            child: ListTile(
              title: Text(s1.FileName),
              subtitle: Padding(
                child: Text(
                  Helper.GetFileSize(s1.Size) + "    " + _remark,
                  style: TextStyle(color: Colors.black38, fontSize: 14.0),
                ),
                padding: EdgeInsets.only(top: 4.0),
              ),
              leading: IconHelper.getFileIcon(s1.FileName, 36.0),
              onTap: () async {
                var fPath = await DownloadHelper.GetLocalFile(s1.Guid);
                print(fPath);
                if (fPath != "") {
                  if (IfView(s1.FileName)) {
                    RouteHelper.ShowPage(
                        context,
                        FileReaderPage(
                          filePath: fPath,
                        ));
                  } else {
                    OpenFile.open(fPath);
                  }
                } else {
                  showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return DialogWidget.getDialog(
                          context, "下载文件", "点击确定将该文件添加到下载目录！", () {
                        DownloadHelper.DownLoad(context, s1);
                      });
                    },
                  );
                }
              },
              onLongPress: () {
                showDialog(
                  context: context,
                  builder: (BuildContext context) {
                    if (u1.Guid != UIData.CurrentUser!.Guid) {
                      return DialogWidget.getAlert(
                          context, "提示", "只能删除自己上传的文件");
                    } else {
                      return DialogWidget.getDialog(
                          context, "删除文件", "点击确定将从服务器中删除该文件！", () {
                        FtpFile.Delete(s1);
                        deleted(s1);
                      });
                    }
                  },
                );
              },
            ),
          ),
          InputWidget.getDivider()
        ]));
  }

  static void fileOnTap(BuildContext context, SpaceItem s1) async {
    var fPath = await DownloadHelper.GetLocalFile(s1.Guid);
    if (fPath == "") {
      var taskID = await DownloadHelper.GetDownLoadTask(s1.Guid);

      if (taskID == null) {
        await DownloadHelper.DownLoad2(context, s1);

        ChannelHelper.showToast("已开始下载，请稍候");
      } else {
        ChannelHelper.showToast("正在下载，请稍候");
      }
    }
    if (fPath != "") {
      if (IfView(s1.FileName)) {
        RouteHelper.ShowPage(
            context,
            FileReaderPage(
              filePath: fPath,
            ));
      } else {
        OpenFile.open(fPath);
      }
    }
  }

  static void fileOnUpdate(String spaceId, String tag) async {
    File file = File((await Helper.getFilePickPath())!);
    var pb1 = file.existsSync();
    if (pb1 == false) {
      SnackBar(
        content: Text("文件不存在！"),
      );
      return;
    }
    var guid = Helper.GetGuid();
    var size1 = file.lengthSync();
    var x1 = new SpaceItem(
        0,
        guid,
        UIData.CurrentUser!.Guid,
        spaceId,
        0,
        file.path,
        Helper.DateTimeNowString(),
        -1,
        size1,
        "",
        "",
        "",
        "",
        "",
        tag);
    var pbx = await SpaceItem.Add(x1);
    if (!pbx) return;
    UploadHelper.UpLoad(file.absolute.path, tag, guid, spaceId);
    ChannelHelper.showToast("已将文件添加到上传列表");
  }

  static Widget fileCard3(BuildContext context, SpaceItem s1) {
    var u1 = CacheHelper.GetUser(s1.UserId);
    String _remark = Helper.DateTimeFormat2(s1.CreateTime);
    if (u1 != null) _remark = _remark + "    " + u1.Name;

    return Container(
        key: Key(s1.Guid),
        color: Colors.white,
        child: Column(children: [
          Container(
            margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
            child: ListTile(
              title: Text(s1.FileName),
              // trailing: Container(
              //   width: 10.0,
              //   height: 10.0,
              //   child: new CircleAvatar(
              //     backgroundColor:
              //         s1.progress == 100 ? Colors.green : Colors.red,
              //   ),
              // ),
              subtitle: Padding(
                child: Text(
                  Helper.GetFileSize(s1.Size) + "    " + _remark,
                  style: TextStyle(color: Colors.black38, fontSize: 14.0),
                ),
                padding: EdgeInsets.only(top: 4.0),
              ),
              leading: IconHelper.getFileIcon(s1.FileName, 36.0),
              onTap: () {
                FileSpaceHelper.fileOnTap(context, s1);
              },
            ),
          ),
          InputWidget.getDivider()
        ]));
  }
}
